package com.bdq.user.application.service.impl;

import cn.hutool.core.convert.Convert;
import com.bdq.common.config.common.TokenInterceptor;
import com.bdq.common.constants.Constant;
import com.bdq.common.entity.UserContext;
import com.bdq.common.utils.IdempotentUtil;
import com.bdq.common.utils.UserContextUtil;
import com.bdq.user.application.model.cmd.LoginCmd;
import com.bdq.user.application.model.cmd.SendLoginVerifyCodeCmd;
import com.bdq.user.application.model.cmd.UserUpdateCmd;
import com.bdq.user.application.model.cmd.ViewRegionLevelUpdateCmd;
import com.bdq.user.application.model.dto.LoginAppDTO;
import com.bdq.user.application.service.UserAppCmdService;
import com.bdq.user.domain.model.agg.UserAgg;
import com.bdq.user.domain.model.dto.req.UserUpdateReqDTO;
import com.bdq.user.domain.model.dto.req.ViewRegionLevelUpdateReqDTO;
import com.bdq.user.domain.service.UserDomainService;
import com.bdq.user.infrastructure.integration.AliYunSmsClient;
import com.bdq.user.domain.model.dto.LoginDomainDTO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author malei
 * @date 2024-05-03 17:17
 */
@Service
public class UserAppCmdServiceImpl implements UserAppCmdService {

    @Resource
    private AliYunSmsClient aliYunSmsClient;

    @Resource
    private UserDomainService userDomainService;

    @Override
    public void sendLoginVerifyCode(SendLoginVerifyCodeCmd cmd) {
        IdempotentUtil.idempotent(String.format(Constant.RedisKeyTemplate.LOGIN_VERIFY_CODE_IDEMPOTENT, cmd.getUserMobile()), 60L);
        aliYunSmsClient.sendLoginVerifyCode(cmd.getUserMobile());
    }

    @Override
    public LoginAppDTO login(LoginCmd cmd) {
        UserAgg userAgg = new UserAgg();
        userAgg.setUserMobile(cmd.getUserMobile());
        userAgg.setLoginVerifyCode(cmd.getVerifyCode());
        LoginDomainDTO loginDomainDTO = userDomainService.login(userAgg);
        return Convert.convert(LoginAppDTO.class, loginDomainDTO);
    }

    @Override
    public Boolean logout() {
        // 1 获取用户信息
        UserContext userContext = UserContextUtil.getLoginUserContext(TokenInterceptor.getToken());
        if (Objects.isNull(userContext)) {
            return true;
        }
        return userDomainService.logout(userContext.getUserMobile());
    }

    @Override
    public Boolean userUpdate(UserUpdateCmd cmd) {
        // 1 获取用户信息
        UserContext userContext = UserContextUtil.getLoginUserContext(TokenInterceptor.getToken());

        // 2 用户聚合
        UserAgg userAgg = new UserAgg();
        userDomainService.userAggFill(userContext.getUserMobile(), userAgg);

        // 3 更新资料
        userDomainService.userUpdate(Convert.convert(UserUpdateReqDTO.class, cmd), userAgg);
        return true;
    }

    @Override
    public Boolean updateViewRegionLevel(ViewRegionLevelUpdateCmd cmd) {
        // 1 获取用户信息
        UserContext userContext = UserContextUtil.getLoginUserContext(TokenInterceptor.getToken());

        // 2 用户聚合
        UserAgg userAgg = new UserAgg();
        userDomainService.userAggFill(userContext.getUserMobile(), userAgg);

        // 3 更新浏览区域级别
        return userDomainService.updateViewRegionLevel(Convert.convert(ViewRegionLevelUpdateReqDTO.class, cmd), userAgg);
    }
}
